(function() {
    var wechatAppId = "wx1f01eabc3968e9d6";

    function initWXConfig() {
        this.nonceStr = this.generateMixed(32);
        this.timestamp = new Date().getTime();
        this.url = window.location.href;
        this.appId = wechatAppId;
        this.host = "http://wx.innjia.com/h5/";
        // this.debug = true;
        this.debug = false;
        // 所有要调用的 API 都要加到这个列表中
        this.jsApiList = [
            'chooseImage',
            'uploadImage',
            'downloadImage'
        ];
        this.signature = '';
        this.getJSApi_ticket();
    }
    initWXConfig.prototype = {
        getJSApi_ticket: function() {
            var self = this;

            $.get('/api/Common/getToken', function(data) {
                var values = data.split(",");
                var ticket = values[1];

                // var signatureString = "jsapi_ticket=" + data.ticket + "&noncestr=" + self.nonceStr + "&timestamp=" + self.timestamp + "&url=" + self.url;
                var signatureString = "jsapi_ticket=" + ticket + "&noncestr=" + self.nonceStr + "&timestamp=" + self.timestamp + "&url=" + self.url;
                self.signature = SHA1(signatureString);
                self.setConfig();
            });
        },
        setConfig: function() {
            var self = this;
            wx.config({
                debug: self.debug,
                appId: self.appId,
                timestamp: self.timestamp,
                nonceStr: self.nonceStr,
                signature: self.signature,
                jsApiList: self.jsApiList
            });
        },
        generateMixed: function(n) {
            var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
            var res = "";
            for (var i = 0; i < n; i++) {
                var id = Math.ceil(Math.random() * 35);
                res += chars[id];
            }
            return res;
        },
        shareWechat: function(title, linkUrl, desc, shareLogoUrl) {
            var self = this;
            shareLogoUrl = "http://" + window.location.hostname + shareLogoUrl;

            wx.onMenuShareTimeline({
                title: title, // 分享标题
                link: linkUrl, // 分享链接
                desc: desc,
                imgUrl: shareLogoUrl, // 分享图标
                success: function() {
                    // alert("shareTimeline  success");
                    // 用户确认分享后执行的回调函数
                },
                cancel: function() {
                    // alert("calcel");
                    // 用户取消分享后执行的回调函数
                }
            });
            wx.onMenuShareAppMessage({
                title: title, // 分享标题
                desc: desc, // 分享描述
                link: linkUrl, // 分享链接
                imgUrl: shareLogoUrl, // 分享图标
                type: '', // 分享类型,music、video或link，不填默认为link
                dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
                success: function() {
                    // 用户确认分享后执行的回调函数
                },
                cancel: function() {
                    // 用户取消分享后执行的回调函数
                }
            });
        },
        chooseImg: function() {
            var _this = this;
            wx.chooseImage({
                count: 9, // 默认9
                sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回选定照片的本地ID列表，localId可以作为img标签的src属性显示图片

                    console.log( localIds );

                    // $.each( localIds,function( i,val ) {
                        _this.uploadImg( localIds[0] );
                    // });
                }
            });
        },
        uploadImg: function(localId) {
            wx.uploadImage({
                localId: localId, // 需要上传的图片的本地ID，由chooseImage接口获得
                isShowProgressTips: 1, // 默认为1，显示进度提示
                success: function (res) {
                    var serverId = res.serverId; // 返回图片的服务器端ID
                    console.log( serverId );
                }
            });
        }
    }
    window.wxinit = new initWXConfig();
    wx.ready(function() {
        $('.id-card-01').on("click",function() {
            window.wxinit.chooseImg();
        })
    });
}).call(this);
